package cn.cai.commonUtils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
@Data
public class Result {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回的信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data;
    //构造方法私有
    private Result(){}

    //设置一些返回的基本情况

    //成功
    public static Result success(){
        Result result = new Result();
        result.success = true;
        result.code = ResultCode.SUCCESS;
        result.message = "请求成功";
        result.data = new HashMap<>();
        return result;
    }
    //失败
    public static Result error(){
        Result result = new Result();
        result.success = false;
        result.code = ResultCode.ERROR;
        result.message = "请求失败";
        result.data = new HashMap<>();
        return result;
    }
    //使用链式编程
    public Result data(String key,Object value){
        this.data.put(key,value);       //向对象中添加数据
        return this;                    //返回
    }

    public Result massage(String value){
        this.setMessage(value);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
}
